﻿/*
 * CanonOscar 0.1
 * Written by (c) agehack a.k.a. GUnitSoldier
 * Released under GNU GPL (http://www.gnu.org/copyleft/gpl.html)
 * 
 * http://www.agehack.pro/oscar
 */
using System;
using System.Security.Cryptography;
using System.IO;
using System.Text;
namespace Agehack
{
    /// <summary>
    /// Class for MD5 hashum generation
    /// </summary>
    public static class MD5Hash
    {
        /// <summary>
        /// Generates hashsum of file.
        /// </summary>
        /// <param name="path">Path to a file.</param>
        /// <param name="cut">Set to true to cut result mode.</param>
        /// <returns>Hashed string</returns>
        public static string ComputeFile(string path, bool cut)
        {
            using (FileStream fs = System.IO.File.OpenRead(path))
            {
                MD5 md5 = new MD5CryptoServiceProvider();
                byte[] fileData = new byte[fs.Length];
                fs.Read(fileData, 0, (int)fs.Length);
                byte[] checkSum = md5.ComputeHash(fileData);
                string result = BitConverter.ToString(checkSum);
                if (cut)
                    result = result.Replace("-", String.Empty);
                return result.ToLower();
            }
        }
        /// <summary>
        /// Generates hashsum of file.
        /// </summary>
        /// <param name="str_src">Source string to hash</param>
        /// <param name="cut">Set to true to cut result mode.</param>
        /// <returns>Hashed string</returns>
        public static string Compute(string str_src, bool cut)
        {
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] checkSum = md5.ComputeHash(Encoding.ASCII.GetBytes(str_src));
            string result = BitConverter.ToString(checkSum);
            if (cut)
                result = result.Replace("-", String.Empty);
            return result.ToLower();
        }
        /// <summary>
        /// Generates hashsum of file.
        /// </summary>
        /// <param name="path">Path to a file.</param>
        /// <returns>Hashed string</returns>
        public static string ComputeFile(string path)
        {
            return ComputeFile(path, true);
        }
        /// <summary>
        /// Generates hashsum of file.
        /// </summary>
        /// <param name="str_src">Source string to hash</param>
        /// <returns>Hashed string</returns>
        public static string Compute(string str_src)
        {
            return Compute(str_src, true);
        }
    }
    public static partial class util
    {
        public static byte[] stringAsBytes(string str)
        {
            byte[] bts = new byte[str.Length];
            for (int i = 0; i < str.Length; i++)
                bts[i] = (byte)(char)str[i];
            return bts;
        }
        public static string xorString(string str)
        {
            /*
             * PHP Code:
             function xorpass($pass)
             {
                 $roast = array(0xF3, 0x26, 0x81, 0xC4, 0x39, 0x86, 0xDB, 0x92, 0x71, 0xA3, 0xB9, 0xE6, 0x53, 0x7A, 0x95, 0x7c);
                 $roasting_pass = '';
                 for ($i=0; $i<strlen($pass); $i++)
                 {
                 $roasting_pass .= chr($roast[$i] ^ ord($pass{$i}));
                 }
                 return($roasting_pass);
             }
            */

            byte[] roast = new byte[] 
            {
                0xF3, 0x26, 0x81, 0xC4, 0x39, 0x86, 0xDB, 0x92, 0x71, 0xA3, 0xB9, 0xE6, 0x53, 0x7A, 0x95, 0x7c
            };
            string roasted = "";
            for (int i = 0; i < str.Length; i++)
                roasted += chr((byte)(roast[i] ^ ord(str[i])));
            return roasted;
        }
        public static char chr(byte asii) { return Encoding.ASCII.GetString(new byte[] { asii })[0]; }
        public static byte ord(char ccd) { return Encoding.ASCII.GetBytes(ccd.ToString())[0]; }
    }
}
